package com.lxb.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxb.staservice.client.UnceterClient;
import com.lxb.staservice.entity.StatisticsDaily;
import com.lxb.staservice.mapper.StatisticsDailyMapper;
import com.lxb.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网站统计日数据 服务实现类
 *
 * @author lixuebin
 * @since 2023-01-29
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UnceterClient ucenterClient;

    /**
     * 统计某天注册人数
     *
     * @param day
     */
    @Override
    public void createStatisticsByDay(String day) {

        //查询该天有没有数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        StatisticsDaily isDaily = baseMapper.selectOne(wrapper);

        //远程调用接口，得到某天注册人数
        Integer integer = ucenterClient.CountRegister(day);

        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(integer);//注册人数
        daily.setCourseNum(RandomUtils.nextInt(100, 200));//每日新增课程数
        daily.setLoginNum(RandomUtils.nextInt(200, 300));//登录数
        daily.setVideoViewNum(RandomUtils.nextInt(200, 300));//每日播放视频数
        daily.setDateCalculated(day);//统计日期

        //保存到数据库
        if (isDaily == null) {
            baseMapper.insert(daily);
        } else {
            isDaily.setRegisterNum(integer);//注册人数
            isDaily.setCourseNum(RandomUtils.nextInt(100, 200));//每日新增课程数
            isDaily.setLoginNum(RandomUtils.nextInt(200, 300));//登录数
            isDaily.setVideoViewNum(RandomUtils.nextInt(200, 300));//每日播放视频数
            isDaily.setDateCalculated(day);//统计日期
            baseMapper.updateById(isDaily);
        }
    }

    /**
     * 展示表格数据,返回两部分数据，日期json数组，数量json数组
     *
     * @param begin
     * @param end
     * @param type
     * @return
     */
    @Override
    public Map<String, Object> getChartData(String begin, String end, String type) {

        //查询条件对象
        QueryWrapper<StatisticsDaily> dailyQueryWrapper = new QueryWrapper<>();
        dailyQueryWrapper.select(type, "date_calculated");
        dailyQueryWrapper.between("date_calculated", begin, end);

        //获取符合查询条件的集合
        List<StatisticsDaily> dailyList = baseMapper.selectList(dailyQueryWrapper);

        //因为返回有两部分数据：日期 和 日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期xlist ，一个数量ylist
        List<String> xlist = new ArrayList<>();
        List<Integer> ylist = new ArrayList<>();

        //封装集合
        for (StatisticsDaily daily : dailyList) {
            xlist.add(daily.getDateCalculated());

            //封装对应查询字段的数量
            if (type.equals("register_num")) {
                ylist.add(daily.getRegisterNum());
            }
            if ("login_num".equals(type)) {
                ylist.add(daily.getLoginNum());
            }
            if ("video_view_num".equals(type)) {
                ylist.add(daily.getVideoViewNum());
            }
            if ("course_num".equals(type)) {
                ylist.add(daily.getCourseNum());
            }

        }

        Map<String, Object> map = new HashMap<>();
        map.put("xlist", xlist);
        map.put("ylist", ylist);

        return map;
    }
}
